package com.app.dao.basesetting;

import com.app.SpringAnnotationDef;
import com.app.dao.BaseDaoImpl;
import com.app.model.*;
import org.hibernate.criterion.Order;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Repository;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 * User: cnprinces
 * Date: 13-1-24
 * Time: 下午4:32
 * To change this template use File | Settings | File Templates.
 */

@Scope(SpringAnnotationDef.SCOPE_PROTOTYPE)
@Repository(SpringAnnotationDef.GOODS_DAO)
public class GoodsDaoImpl extends BaseDaoImpl<Goods_Type, Integer> implements GoodsDao {
    public GoodsDaoImpl() {
        super(Goods_Type.class);
    }

    @Override
    public List<Goods_Info> getGoodsTypeByParentId(Goods_Type goodsType) {
        String hql = "from Goods_Info where typeParentId=" + goodsType.getId();
        return getSession().createQuery(hql).list();
    }

    @Override
    public List<Goods_Info> getGoodsTypeById(Goods_Type goodsType) {
        String hql = "from Goods_Info where typeId=" + goodsType.getId();
        return getSession().createQuery(hql).list();
    }

    @Override
    public Goods_Info getGoodsInfo(Integer id) {
        return (Goods_Info) getSession().get(Goods_Info.class, id);
    }

    @Override
    public void deleteGoodsInfo(Goods_Info goodsInfo) {
        String sql = "delete from Goods_Info where id=" + goodsInfo.getId();
        getJdbcTemplate().execute(sql);
    }

    @Override
    public List<Goods_Package> getGoodsPackageList(boolean b) {
        if (b) {
            return (List<Goods_Package>) getSession().createCriteria(Goods_Package.class).addOrder(Order.asc("id")).list();
        } else {
            String hql = "from Goods_Package where pStatus=0";
            return getSession().createQuery(hql).list();
        }
    }

    @Override
    public void deleteGoodsPackage(Goods_Package goodsPackage) {
        String sql = "delete from Goods_Package where id=" + goodsPackage.getId();
        getJdbcTemplate().execute(sql);
    }

    @Override
    public Goods_Package getGoodsPackageById(Goods_Package goodsPackage) {
        return (Goods_Package) getSession().get(Goods_Package.class, goodsPackage.getId());
    }

    @Override
    public List<Goods_Info> loadGoodsInfoList() {
        return (List<Goods_Info>) getSession().createCriteria(Goods_Info.class).addOrder(Order.asc("id")).list();
    }

    @Override
    public void deleteGoodsInfoToPackageById(Integer id) {
        String sql = "delete from Goods_InfoToPackage where id=" + id;
        getJdbcTemplate().execute(sql);
    }

    @Override
    public void deleteGoodsInfoToPackageByPackageId(Integer id) {
        String sql = "delete from Goods_InfoToPackage where GoodsPackageId=" + id;
        getJdbcTemplate().execute(sql);
    }

    @Override
    public List<Goods_InfoToPackage> getGoodsInfoToPackageByPackageId(Integer id) {
        String hql = "from Goods_InfoToPackage where GoodsPackageId=" + id;
        return getSession().createQuery(hql).list();
    }
}
